﻿//string扩展
String.prototype.trim = function () {
    return this.replace(/(^[\s|　]*)|([\s|　]*$)/g, ""); 
}
//date扩展格式方法
Date.prototype.pattern = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1, //月份        
        "d+": this.getDate(), //日        
        "h+": this.getHours() % 12 == 0 ? 12 : this.getHours() % 12, //小时        
        "H+": this.getHours(), //小时        
        "m+": this.getMinutes(), //分        
        "s+": this.getSeconds(), //秒        
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度        
        "S": this.getMilliseconds() //毫秒        
    };
    var week = {
        "0": "\u65e5",
        "1": "\u4e00",
        "2": "\u4e8c",
        "3": "\u4e09",
        "4": "\u56db",
        "5": "\u4e94",
        "6": "\u516d"
    };
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    if (/(E+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "\u661f\u671f" : "\u5468") : "") + week[this.getDay() + ""]);
    }
    for (var k in o) {
        if (new RegExp("(" + k + ")").test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        }
    }
    return fmt;
}
//判断数组中存在item否
function arrstrContrastItem(arr, item) {
    for (var i = 0; i < arr.length; i++) {
        if (arr[i] == item) return true; ;
    }

}
//转反json中的data对象为所需的时间格式
function JsonDateToDate(json,toStr) {
    var _date = eval("new " + json);

    var _str = "yyyy-MM-dd hh:mm";

    if (toStr != undefined) {
        _str = toStr;
    }

    return _date.pattern(_str);
}

//过滤空用户
function nnUserSet(value) {

    if (value == null) { return "匿名用户"; }
    return value;
}

//提取URL参数
function getURLParams(){
    var URLParams = {};

    if (document.location.search.substr(1) == "") return;
    var aParams = document.location.search.substr(1).split('&');

        for (i = 0; i < aParams.length; i++) {
            var aParam = aParams[i].split('=');
            URLParams[aParam[0]] = aParam[1];
        }
        return URLParams;
    }
    //取得登录用户
    function xxx() { }
    function getLoginUser() {
        return { name: document.getElementById("loginUserId").value, id: document.getElementById("loginUserName").value };
    }
    //综合字符串检验
    function strCheck(str, type) {
        var result = false;
        switch (type) {
            case "":

                result = result || (str == "");

                break;
            case "xx":
                break;
            default:

        }
        return result;
    }
    //提示
    function sysAlert(code) {
        var str = "";
        switch (code) {
            case 1000:
                str = "内容不能为空";
                break;
            case 1001:
                str = "更新出错"
                break;
            default:
                str=code
        }
        
        alert(str)
    }
    function sysConfirm(str) {
        if(!str) str="确认此操作吗？"
        return confirm(str)


    }
    function tranAjaxCallBackJson(json) {
        json = eval(json)
        if (json.result == 'ok') {
            return json.content;
        } else {
            sysAlert(json.content);
            return "";
        }
    }
    //ObjectId 
    /*
    *
    * Copyright (c) 2011 Justin Dearing (zippy1981@gmail.com)
    * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
    * and GPL (http://www.opensource.org/licenses/gpl-license.php) version 2 licenses.
    * This software is not distributed under version 3 or later of the GPL.
    *
    * Version 1.0.0
    *
    */

    /**
    * Javascript class that mimics how WCF serializes a object of type MongoDB.Bson.ObjectId
    * and converts between that format and the standard 24 character representation.
    */
    var ObjectId = (function () {
        var increment = 0;
        var pid = Math.floor(Math.random() * (32767));
        var machine = Math.floor(Math.random() * (16777216));

        if (typeof (localStorage) != 'undefined') {
            var mongoMachineId = parseInt(localStorage['mongoMachineId']);
            if (mongoMachineId >= 0 && mongoMachineId <= 16777215) {
                machine = Math.floor(localStorage['mongoMachineId']);
            }
            // Just always stick the value in.
            localStorage['mongoMachineId'] = machine;
            document.cookie = 'mongoMachineId=' + machine + ';expires=Tue, 19 Jan 2038 05:00:00 GMT'
        }
        else {
            var cookieList = document.cookie.split('; ');
            for (var i in cookieList) {
                var cookie = cookieList[i].split('=');
                if (cookie[0] == 'mongoMachineId' && cookie[1] >= 0 && cookie[1] <= 16777215) {
                    machine = cookie[1];
                    break;
                }
            }
            document.cookie = 'mongoMachineId=' + machine + ';expires=Tue, 19 Jan 2038 05:00:00 GMT';

        }

        return function () {
            if (!(this instanceof ObjectId)) {
                return new ObjectId(arguments[0], arguments[1], arguments[2], arguments[3]).toString();
            }

            if (typeof (arguments[0]) == 'object') {
                this.timestamp = arguments[0].timestamp;
                this.machine = arguments[0].machine;
                this.pid = arguments[0].pid;
                this.increment = arguments[0].increment;
            }
            else if (typeof (arguments[0]) == 'string' && arguments[0].length == 24) {
                this.timestamp = Number('0x' + arguments[0].substr(0, 8)),
            this.machine = Number('0x' + arguments[0].substr(8, 6)),
            this.pid = Number('0x' + arguments[0].substr(14, 4)),
            this.increment = Number('0x' + arguments[0].substr(18, 6))
            }
            else if (arguments.length == 4 && arguments[0] != null) {
                this.timestamp = arguments[0];
                this.machine = arguments[1];
                this.pid = arguments[2];
                this.increment = arguments[3];
            }
            else {
                this.timestamp = Math.floor(new Date().valueOf() / 1000);
                this.machine = machine;
                this.pid = pid;
                if (increment > 0xffffff) {
                    increment = 0;
                }
                this.increment = increment++;

            }
        };
    })();

    ObjectId.prototype.getDate = function () {
        return new Date(this.timestamp * 1000);
    }

    /**
    * Turns a WCF representation of a BSON ObjectId into a 24 character string representation.
    */
    ObjectId.prototype.toString = function () {
        var timestamp = this.timestamp.toString(16);
        var machine = this.machine.toString(16);
        var pid = this.pid.toString(16);
        var increment = this.increment.toString(16);
        return '00000000'.substr(0, 6 - timestamp.length) + timestamp +
           '000000'.substr(0, 6 - machine.length) + machine +
           '0000'.substr(0, 4 - pid.length) + pid +
           '000000'.substr(0, 6 - increment.length) + increment;
    }
    ObjectId.byString = function (oid) {
        var timestamp = oid.Timestamp.toString(16);
        var machine = oid.Machine.toString(16);
        var pid = oid.Pid.toString(16);
        var increment = oid.Increment.toString(16);
        return '00000000'.substr(0, 6 - timestamp.length) + timestamp +
           '000000'.substr(0, 6 - machine.length) + machine +
           '0000'.substr(0, 4 - pid.length) + pid +
           '000000'.substr(0, 6 - increment.length) + increment;
    }
